<?php
class img_delay_loader_modifier{
    function delay( &$content ){
		$system = &$GLOBALS['system'];
		$baseurl = $system->base_url();
        $content = preg_replace('/(<img[^\>]*\s+src=)(["\'|][^"\'|]+["\'|])(.*?\>)/is'
							,'$1"'.$baseurl.'statics/transparent.gif" init_src=$2$3',$content);
		$scripts = <<<EOF
<script>
window.addEvent('domready',function(){

        var forInit = $$('img[init_src]');
        var win = this;
        
        (function(){
            if(!forInit.length)return false;
            var loop = arguments.callee;
            
            forInit.each(function(item,index){
                if(!item)return;
                if((item.getPosition(win).y-win.getScrollTop())<win.getSize().y){
                    item.set('src',item.get('init_src')).removeProperty('init_src')
                    forInit.erase(item);
                }
                
                if(index == (forInit.length-1)){loop.delay(200);}
               
            });
        })();

});
</script>
EOF;
		$content = str_replace('</body>',$scripts.'</body>',$content);
		return $content;
    }
}
